﻿const defaultResult = 0;
let currentResult = defaultResult;
let logEntries=[];


// Gets input from input filed
function getUserNumberInput(){
    return parseInt(userInput.value);
}

// Generates and writes calculation log
function createAndWriteOutput(operator,resultBeforeCale,calcNumber){
    const calcDescription = `${resultBeforeCale} ${operator} ${calcNumber}`;
    outputResult(currentResult,calcDescription);
}

function writeLog(operationIdentifier,prevResult,operationNumber,newResult){
    const logEntry = {
        operation :operationIdentifier,
        prevResult: prevResult,
        number:operationNumber,
        result:newResult,
    };
    logEntries.push(logEntry);
}

function calculateResult(calculationType){
    const enterNumber = getUserNumberInput();
    if(calculationType !== 'ADD' &&
        calculationType !=='SUBTRACT' &&
        calculationType !=='MULTIPLY' &&
        calculationType !=='DIVIDE'  ||
        !enterNumber){
        return;
    }

    const initialResult = currentResult;
    let mathOperator;
    if(calculationType ==='ADD'){
        currentResult +=  enterNumber;
        mathOperator = '+';
    }else if(calculationType==='SUBTRACT'){
        currentResult -=enterNumber;
        mathOperator = '-';
    }else if(calculationType==='MULTIPLY'){
        currentResult *=enterNumber;
        mathOperator = '*';
    }else if(calculationType === 'DIVIDE'){
        currentResult /=enterNumber;
        mathOperator = '/';
    }

    createAndWriteOutput(mathOperator,initialResult,enterNumber);
    writeLog(calculationType,initialResult,enterNumber,currentResult);
}

function add(){
    calculateResult('ADD');
}

function subtract(){
    calculateResult('SUBTRACT');
}

function multiply(){
    calculateResult('MULTIPLY');
}

function divide(){
    calculateResult('DIVIDE');
}

addBtn.addEventListener('click',add);
subtractBtn.addEventListener('click',subtract);
multiplyBtn.addEventListener('click',multiply);
divideBtn.addEventListener('click',divide);
